package com.liveneo.system.base.utils;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Map处理工具类
 * 
 * @author fucy
 *
 */
public class MapUtils {
	
	/**
	 * 根据多个字符串参数构建一个Map，
	 * 
	 * @param args
	 * @return
	 */
	public static Map<String, String> buildMap(String ...args){
		if(args.length%2 != 0){
			throw new RuntimeException("build Map Error, paramters must in pairs");
		}
		
		Map<String, String> resultMap = new HashMap<String, String>();
		
		for(int i = 0; i < args.length/2; i++){
			resultMap.put(args[i*2], args[i*2+1]);
		}
		
		return resultMap;
	}
	
	/**
	 * 根据多个字符串参数构建一个有序Map，
	 * 
	 * @param args
	 * @return
	 */
	public static Map<String, String> buildOrderMap(String ...args){
		if(args.length%2 != 0){
			throw new RuntimeException("build Map Error, paramters must in pairs");
		}
		
		Map<String, String> resultMap = new LinkedHashMap<String, String>();
		
		for(int i = 0; i < args.length/2; i++){
			resultMap.put(args[i*2], args[i*2+1]);
		}
		
		return resultMap;
	}
	
	/**
	 * 根据多个字符串参数构建一个Map<String, Object>，
	 * 
	 * @param args 参数需要字符串间隔
	 * @return
	 */
	public static Map<String, Object> buildMap(Object ...args){
		if(args.length%2 != 0){
			throw new RuntimeException("build Map Error, paramters must in pairs");
		}
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		for(int i = 0; i < args.length/2; i++){
			resultMap.put((String)args[i*2], args[i*2+1]);
		}
		
		return resultMap;
	}
	
	/**
	 * 根据多个字符串参数构建一个有序Map<String, Object>，
	 * 
	 * @param args 参数需要字符串间隔
	 * @return
	 */
	public static Map<String, Object> buildOrderMap(Object ...args){
		if(args.length%2 != 0){
			throw new RuntimeException("build Map Error, paramters must in pairs");
		}
		
		Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
		
		for(int i = 0; i < args.length/2; i++){
			resultMap.put((String)args[i*2], args[i*2+1]);
		}
		
		return resultMap;
	}
}
